﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SundUnderviser.ControlLayer;
using SundUnderviser.ModelLayer;

namespace SundUnderviser.GUILayer
{
    public partial class UnderviserForm : Form
    {
        private List<Modul> modulList;
        private List<Tema> temaList;
        private List<Patient> patientList;
        private List<Underviser> underviserList;
        private List<Stue> stueList;
        private List<Seng> sengList;
        private Modul valgtModul;
        private Stue valgtStue;

        public UnderviserForm()
        {
            InitializeComponent();
            UnderviserControler undvCtr = new UnderviserControler();
            modulList = undvCtr.hentAlleModuler();
            foreach (Modul modulObj in modulList)
            {
                comboBoxModul.Items.Add(modulObj.Navn);

            }
            stueList = undvCtr.hentAlleStuer();
            if (stueList != null)
            {
                foreach (Stue stueObj in stueList)
                {
                    comboBoxStue.Items.Add(stueObj.Stuenr);

                }
            }
            patientList = undvCtr.hentAllePatienter();
            if (patientList != null)
            {
                foreach (Patient patientObj in patientList)
                {
                    comboBoxPatient.Items.Add(patientObj.Navn);

                }
            }
            underviserList = undvCtr.hentAlleUnderviser();
            if (underviserList != null)
            {
                foreach (Underviser underviserObj in underviserList)
                {
                    comboBoxUnderviser.Items.Add(underviserObj.Navn);

                }
            }
        }

        private void UnderviserForm_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            UnderviserControler undvCtr = new UnderviserControler();
            modulList = undvCtr.hentAlleModuler();
            comboBoxPatient.Items.Clear();
            comboBoxModul.Items.Clear();
            comboBoxUnderviser.Items.Clear();
            comboBoxStue.Items.Clear();
            foreach (Modul modulObj in modulList)
            {
                comboBoxModul.Items.Add( modulObj.Navn);
        
            }
            patientList = undvCtr.hentAllePatienter();
            if(patientList != null)
            {
             foreach (Patient patientObj in patientList)
             {
                 comboBoxPatient.Items.Add(patientObj.Navn);

             }
            }
            underviserList = undvCtr.hentAlleUnderviser();
            if (underviserList != null)
            {
                foreach (Underviser underviserObj in underviserList)
                {
                    comboBoxUnderviser.Items.Add(underviserObj.Navn);

                }
            }
            stueList = undvCtr.hentAlleStuer();
            if (stueList != null)
            {
                foreach (Stue stueObj in stueList)
                {
                    comboBoxStue.Items.Add(stueObj.Stuenr);

                }
            }
            
            lektionPanel.Visible = true;
        }

        private void comboBoxModul_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxTema.Items.Clear();
            comboBoxPatient.Items.Clear();
            UnderviserControler undvCtr = new UnderviserControler();
            int index = comboBoxModul.SelectedIndex;
            temaList = undvCtr.hentAlleTemaer(modulList.ElementAt(index).Modulnr);
            valgtModul = modulList.ElementAt(index);
            foreach (Tema temaObj in temaList)
            {
                comboBoxTema.Items.Add(temaObj.Navn);

            }
            patientList = undvCtr.hentAllePatienter(modulList.ElementAt(index).Modulnr);
            if (patientList != null)
            {
                foreach (Patient patientObj in patientList)
                {
                    comboBoxPatient.Items.Add(patientObj.Navn);

                }
            }
            else
            {
                labelFejl.Text = "Der findes ingen patienter til dette modul, opret patient først";
            }

        }

        private void comboBoxStue_SelectedIndexChanged(object sender, EventArgs e)
        {
            int stueIndex = comboBoxStue.SelectedIndex;
            comboBoxSeng.Items.Clear();
            UnderviserControler undvCtr = new UnderviserControler();
            sengList = undvCtr.hentAlleSenge(stueList.ElementAt(stueIndex).Stuenr);
            valgtStue = stueList.ElementAt(stueIndex);
            if (sengList != null)
            {
                foreach (Seng sengObj in sengList)
                {
                    comboBoxSeng.Items.Add(sengObj.Sengnr);

                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            PatientForm patientForm = new PatientForm();
            patientForm.Show();

        }

        private void lektionPanel_Paint(object sender, PaintEventArgs e)
        {

        }
        //opret lektion
        private void button3_Click(object sender, EventArgs e)
        {
            String dato = dateTimePicker1.Text.ToString();
            DateTime dato1 = new DateTime();
            dato1 = DateTime.Parse(dato);
               
           
            int index = comboBoxTema.SelectedIndex;
            Tema temaObj = temaList.ElementAt(index);
            
            index = comboBoxPatient.SelectedIndex;
            Patient patienObj = patientList.ElementAt(index);
            index = comboBoxSeng.SelectedIndex;
            Seng sengObj = sengList.ElementAt(index);
            index = comboBoxUnderviser.SelectedIndex;
            Underviser undvObj = underviserList.ElementAt(index);
            String beskrivelse = richTextBox1.Text;
            UnderviserControler  ctrlUndv = new UnderviserControler();
            index = comboBoxPatient.SelectedIndex;
            Patient patientObj = patientList.ElementAt(index);
            index = comboBox1.SelectedIndex;
            string startid = "830";
            string sluttid = "1000";
            if (index == 1)
            {
                startid = "1015";
                sluttid = "1200";
            }
            if (index == 2)
            {
                startid = "1215";
                sluttid = "1400";
            }

            int rc = ctrlUndv.opretLektion(dato1, valgtModul, temaObj, valgtStue, sengObj, undvObj, beskrivelse, patientObj, startid, sluttid);
            if (rc == 1)
            {//oprettelse er gaaet god felter blankes 
                comboBox1.Text = "";
                comboBoxPatient.Text = "";
                comboBoxPatient.Items.Clear();
                comboBoxModul.Text = "";
                comboBoxModul.Items.Clear();
                comboBoxUnderviser.Items.Clear();
                comboBoxUnderviser.Text = "";
                comboBoxStue.Items.Clear();
                comboBoxStue.Text = "";
                comboBoxTema.Text = "";
                comboBoxSeng.Items.Clear();
                comboBoxSeng.Text = "";
                richTextBox1.Text = "";
                labelFejl.Text = "Lektionen er oprettet";


            }
            else
            {
                labelFejl.Text = "Der er sket en fejl i oprettelse af lektionen";
            }

            
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {

            String dato = monthCalendar1.SelectionRange.Start.ToString();
            Console.WriteLine("valgt dato " + dato);
            DateTime dato1 = new DateTime();
            dato1 = DateTime.Parse(dato);
            UnderviserControler undvCtr = new UnderviserControler();
            List<Lektion> listLektion = undvCtr.hentLektionser(dato1);
            if (listLektion != null)
            {
                listView1.Items.Clear();
                foreach (Lektion lekObj in listLektion)
                {
                    listView1.Items.Add(lekObj.ModulObj.Navn + " " + lekObj.TemaObj.Navn + " "+ lekObj.PatientObj.Navn);
                }

            }
            
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
